﻿package com.projects.xenia.journals_activities;


import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;

import com.projects.xenia.R;
import com.projects.xenia.documents_activities.Activity_Payment;
import com.projects.xenia.documents_classes.Documents_Payment;
import com.projects.xenia.documents_classes.Documents_Visit;
import com.projects.xenia.global_modules.MainGlobalModule;



public class Activity_Journal_Payments extends JournalActivity {

	@Override
	public void SetIconAndDescr() {
		
		head_ico.setImageResource(R.drawable.st_payments);
		head_text.setText("Оплаты");
		
	}
	
	@Override	
	public void GetJournalCursor () {		
	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");		
		String JournalStartDateValueString = sdf.format(JournalStartDateValue);
		String JournalStopDateValueString = sdf.format(JournalStopDateValue);
		
		Cursor TotalCursor;
		
		if (Journ_Customer.getTag()==null) {
			TotalCursor = MainGlobalModule.DBE.rawQuery("select sum (a.total_paymentsum) from xdoc_payments_h a where (date(a.date_create) between date(?) and date(?)) and (a.state in (1,5,13,29))", new String []{JournalStartDateValueString,JournalStopDateValueString});
			JournalCursor = MainGlobalModule.DBE.rawQuery("select a.id,a.docnum,date(a.date_create) as date_create ,a.state,a.customers_id,a.total_paymentsum,b.descr,b.outletdescr,b.outletadress from xdoc_payments_h a left outer join xref_customers b on a.customers_id = b.id where date(a.date_create) between date(?) and date(?) order by a.date_create,a.docnum", new String []{JournalStartDateValueString,JournalStopDateValueString});			
		}
		else
		{
			TotalCursor = MainGlobalModule.DBE.rawQuery("select sum (a.total_paymentsum) from xdoc_payments_h a where a.customers_id = ? and (date(a.date_create) between date(?) and date(?)) and (a.state in (1,5,13,29))", new String []{Journ_Customer.getTag().toString(),JournalStartDateValueString,JournalStopDateValueString});
			JournalCursor = MainGlobalModule.DBE.rawQuery("select a.id,a.docnum,date(a.date_create) as date_create ,a.state,a.customers_id,a.total_paymentsum,b.descr,b.outletdescr,b.outletadress from xdoc_payments_h a left outer join xref_customers b on a.customers_id = b.id where a.customers_id = ? and date(a.date_create) between date(?) and date(?) order by a.date_create,a.docnum", new String []{Journ_Customer.getTag().toString(),JournalStartDateValueString,JournalStopDateValueString});	
		}

		if (TotalCursor.getCount()>0) {				
			TotalCursor.moveToFirst();
			JournalTotalSum.setText(String.format("Всего:%.2f",TotalCursor.getDouble(0)));
			TotalCursor.close();			
		}
		else
			{
			JournalTotalSum.setText(String.format("Итого: %.2f",0.00));
			}	
		
	}

    

    
	@Override
	public void StartDocumentsDetailsJournalActivity() {
	
		
	}

	@Override
	public void CreateOpenDocument() {
		
		if (Journ_Customer.getTag()!=null) {
			Documents_Visit DocVisit = new Documents_Visit ();
			if (DocVisit.CheckNewVisitsExistsByCustomerId(String.valueOf(Journ_Customer.getTag()))) {
				Toast.makeText(this, "Нельзя создать документ вне визита !!!", Toast.LENGTH_SHORT).show();
				return;
			}		
		}

    	Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(Ctx, Activity_Payment.class);
    	DocumentActivity.putExtra("CustomerId", Journ_Customer.getTag()==null?null:String.valueOf(Journ_Customer.getTag()));    	
    	((Activity)Ctx).startActivityForResult(DocumentActivity, 0);
    	
	}

	@Override
	public void OpenDocument() {
		
		Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(Ctx, Activity_Payment.class);
    	DocumentActivity.putExtra("DocId", id);    	
    	((Activity)Ctx).startActivityForResult(DocumentActivity, 0);
		
	}

	@Override
	public void DeleteDocument() {
		
		Documents_Payment Doc = new Documents_Payment ();
		Doc.OpenDoc(id);
		Doc.DeleteDoc();
		RefreshList();
		
	}

	@Override
	public void JournIcoEvents() {
		// TODO Auto-generated method stub
		
	}
	
}
